package com.lw.syn;

/**
 * @author LW
 * @2020/8/18 9:11
 */
//不安全的买票
public class UnsafeBuyticket {


    public static void main(String[] args) {
        BuyTicket buyTicket = new BuyTicket();
        new Thread(buyTicket,"星星").start();
        new Thread(buyTicket,"小狗").start();
        new Thread(buyTicket,"黄牛").start();


    }


}


class BuyTicket implements Runnable{

    //票数
    private int ticketNums = 10;
    boolean flag = true;
    @Override
    public void run() {
        //买票
        while (flag){
            buy();
        }

    }


    private synchronized void buy(){
        //判断是否有票
        if(ticketNums<=0){
            return;
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //买票
        System.out.println(Thread.currentThread().getName()+"拿到了****"+ticketNums--);
    }
}